﻿using DevExpress.XtraEditors;
using ImageDefine;
using ProjectModule;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImageTaskModule
{
    public static  class ImageTaskManager
    {
        public static ImageTaskCenter taskCenter;
        public static ImageTaskCenter taskCenterGlobal;

        public static void Init()
        {
            taskCenter = ImageTaskCenter.Load(ProjectModule.ProjectManager.ProjectPath + @"/ImageTaskSetting/");
            taskCenterGlobal= ImageTaskCenter.Load(Application.StartupPath + @"/ImageTaskSetting/");
            FrameManager.FrameManager.saveProject += FrameManager_saveProject;
            FrameManager.FrameManager.saveAsProject += FrameManager_saveAsProject;
            FrameManager.FrameManager.openProject += FrameManager_openProject;
        }
        private static bool FrameManager_openProject()
        {
            taskCenter = ImageTaskCenter.Load(ProjectModule.ProjectManager.ProjectPath + @"/ImageTaskSetting/");
            return true;
        }

        private static bool FrameManager_saveAsProject()
        {
            string strSourFilePath = taskCenter.strSettingPath;
            string strTargetFilePath = ProjectModule.ProjectManager.ProjectPath + @"\ImageTaskSetting\";
            try
            {
                CopyDirectory(strSourFilePath, strTargetFilePath);
            }
            catch
            {

            }
            taskCenter.Save();
            return true;
        }

        private static bool FrameManager_saveProject()
        {
            taskCenter.Save();
            taskCenterGlobal.Save();
            return true;
        }
        private static void CopyDirectory(string srcdir, string desdir)
        {

            string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1);
            string desfolderdir = desdir + "\\" + folderName;
            if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
            {
                desfolderdir = desdir + folderName;
            }

            string[] filenames = Directory.GetFileSystemEntries(srcdir);



            foreach (string file in filenames)// 遍历所有的文件和目录 
            {

                if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件 
                {



                    string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1);

                    if (!Directory.Exists(currentdir))
                    {

                        Directory.CreateDirectory(currentdir);

                    }



                    CopyDirectory(file, desfolderdir);

                }



                else // 否则直接copy文件 
                {

                    string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);



                    srcfileName = desfolderdir + "\\" + srcfileName;





                    if (!Directory.Exists(desfolderdir))
                    {

                        Directory.CreateDirectory(desfolderdir);

                    }





                    File.Copy(file, srcfileName);

                }

            }//foreach  

        }//function end

        public static Form OpenTaskCenterSettingForm()
        {
            FormTaskCenterSetting frmSetting = new FormTaskCenterSetting();
            frmSetting.TopLevel = false;
            return frmSetting;
        }
        public static Form OpenTaskItemSettingForm()
        {
            FormTaskItemSetting frmSetting = new FormTaskItemSetting();
            frmSetting.TopLevel = false;
            return frmSetting;
        }
        public static bool RunTask(string strTaskName, SmartDisplayControl.SmartDisplayControl display)
        {
            try
            {
                
                if (taskCenterGlobal.dicTasks.ContainsKey(strTaskName) == false && taskCenter.dicTasks.ContainsKey(strTaskName) == false)
                {
                    throw (new Exception("没有名字为" + strTaskName + "图像任务"));
                }
                if (taskCenterGlobal.dicTasks.ContainsKey(strTaskName))
                {
                    taskCenter.dicTasks[strTaskName].Run(display);
                }
                else
                {
                    if (taskCenter.dicTasks.ContainsKey(strTaskName))
                    {
                        taskCenter.dicTasks[strTaskName].Run(display);
                    }
                    else
                    {

                    }
                }
            }
            catch (Exception ex)
            {
               XtraMessageBox.Show(ex.Message);
            }
            return true;
        }
        public static ImageTask ImageTask(string strTaskName)
        {
            ImageTask taskTemp = null;
            try
            {
                if (taskCenterGlobal.dicTasks.ContainsKey(strTaskName) == false && taskCenter.dicTasks.ContainsKey(strTaskName) == false)
                {
                    throw (new Exception("没有名字为" + strTaskName + "图像任务"));
                }
                if (taskCenterGlobal.dicTasks.ContainsKey(strTaskName))
                {
                    taskTemp = taskCenterGlobal.dicTasks[strTaskName];
                }
                else
                {
                    taskTemp = taskCenter.dicTasks[strTaskName];
                }

            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
            return taskTemp;
        }
    }
}
